package com.asiainfo.iam.job;

import com.asiainfo.iam.task.VerifyTask;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class SignatureVerifyJob {
    private static final Logger log = LoggerFactory.getLogger(SignatureVerifyJob.class);
    @Autowired
    private VerifyTask verifyTask;

    /**
     * 定时验签任务
     */

    @XxlJob(value = "verifyTask")
    public void signatureVerifyHandler() throws Exception {
        log.info("=====开始执行验签任务=====");
        try {
            //1.执行验签逻辑
            Map<Integer, Integer> resMap = executeSignatureVerify();
            //2.遍历验签结果
            for(Map.Entry<Integer, Integer> entry : resMap.entrySet()){
                Integer key = entry.getKey();
                Integer value = entry.getValue();
                if (value == 0){
                    log.info("用户id:{},验签成功！", key);
                }else {
                    log.info("用户id:{},验签失败！", key);
                    // TODO 告警
                }
            }


        }catch (Exception e){
            log.error("定时验签任务执行异常",e);
            throw e;
        }finally {
            log.info("=====验签任务执行完毕=====");
        }
        log.info("验签任务执行完毕");
    }

    /**
     * 验签逻辑
     */
    private Map<Integer, Integer> executeSignatureVerify() {
        log.info("执行具体的验签逻辑...");
        // 执行验签任务
        Map<Integer, Integer> map = verifyTask.verifyTask();
        return map; // 返回验签结果
    }
}
